package cn.huyanbing.visual.spring.research.service.drools;

import cn.huyanbing.visual.spring.research.dao.RulesDaoMock;
import cn.huyanbing.visual.spring.research.drools.entity.Calculation;
import cn.huyanbing.visual.spring.research.drools.entity.CreditCardApplyInfo;
import cn.huyanbing.visual.spring.research.drools.entity.InsuranceInfo;
import cn.huyanbing.visual.spring.research.drools.entity.Order;
import cn.huyanbing.visual.spring.research.drools.utils.KieSessionUtils;
import cn.huyanbing.visual.spring.research.entity.Message;
import cn.huyanbing.visual.spring.research.entity.Rules;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Results;
import org.kie.api.runtime.ClassObjectFilter;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongProject middleware
 * @BelongPackage cn.huyanbing.visual.spring.research.service.drools
 * @Description TODO
 * @ClassName RuleService
 * @Author huyangbing
 * @Date 2025/2/6 17:06
 * @Version 1.0.0
 */
@Service
public class RuleService {
    @Autowired
    private KieBase kieBase;

//    @Autowired
//    private RulesDao rulesDao;

    @Autowired
    private RulesDaoMock rulesDao;

    public Order executeRule(Order order){
        System.out.println(order);
        KieSession kieSession = kieBase.newKieSession();
        kieSession.insert(order);
        kieSession.fireAllRules();
        kieSession.dispose();

        return order;
    }





    public String executeRuleDao(Integer id) {
        String rules = "";
        Rules ru = rulesDao.getById(id);
        if (ru != null && ru.getRules() != null) {
            rules = ru.getRules();
        }

        KieServices kieServices = KieServices.Factory.get();
        KieFileSystem kfs = kieServices.newKieFileSystem();
        kfs.write("src/main/resources/rules/rules.drl", rules.getBytes());
        KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
        Results results = kieBuilder.getResults();
        if (results.hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
            System.out.println(results.getMessages());
            throw new IllegalStateException("### errors ###");
        }
        KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
        KieBase kieBase = kieContainer.getKieBase();
        KieSession ksession = kieBase.newKieSession();

        Message message = new Message();
        message.setStatus("0");
        ksession.insert(message);
        ksession.fireAllRules();

        String result= null;
        for (Object per : ksession.getObjects(new ClassObjectFilter(Message.class))) {
            if (((Message) per).getStatus().equals("0")) {
                result = ((Message) per).getContent();
            }
        }
        return result;
    }


    //个人所得税计算
    public Calculation calculate(Calculation calculation){
        KieSession kieSession = kieBase.newKieSession();
        kieSession.insert(calculation);
        kieSession.fireAllRules();
        kieSession.dispose();
        return calculation;
    }



    //调用Drools规则引擎实现信用卡申请
    public CreditCardApplyInfo creditCardApply(CreditCardApplyInfo creditCardApplyInfo){
        KieSession session = kieBase.newKieSession();
        session.insert(creditCardApplyInfo);
        session.fireAllRules();
        session.dispose();
        return creditCardApplyInfo;
    }


    public List<String> insuranceInfoCheck(InsuranceInfo insuranceInfo) throws Exception{
        KieSession session = KieSessionUtils.getKieSessionFromXLS("/Users/huyangbing/Documents/workspace/workspace2024/open-source/02/middleware/spring-research/src/main/resources/rules/decision/tables/insuranceInfoCheck.xls");
        session.getAgenda().getAgendaGroup("sign").setFocus();

        session.insert(insuranceInfo);

        List<String> listRules = new ArrayList<>();
        session.setGlobal("listRules", listRules);

        session.fireAllRules();
        session.dispose();

        return listRules;
    }


}
